package Serwer;

import java.io.IOException;
import java.net.Socket;

public class Admin extends Gracz implements Runnable{
	Server mSerwer;
	int kolejna;
	
	Admin(Socket x, Server y) {
		super(x);
		mSerwer = y;
	}
	
	public int czyKolejna(){
		if( kolejna == 0) {
			return 0;
		}
		if( kolejna == 1) {
			kolejna = 0;
			return 1;
		}
		kolejna = 0;
		return -1;
	}
	
	public synchronized void GBAll(Karta x[][]) {
		if(mRoom != null) {
			String temp = "";
			temp = "G_BALL_";
			int k = x.length;
			for(int i = 0; i < k ; i++ ) {
				if(mRoom.Gracze[i] != null) {
					if( mRoom.Gracze[i].getClass() == Admin.class )
					{
						temp = temp + "[" + i + ",";
					}
					else {
						temp = temp + "[B" + i + ",";
					}
					for(int j = 0; j < 5 ; j++) {
						temp = temp + x[i][j].Kolor + "," + x[i][j].Wartosc + ";" ;
					}
					temp += "]";
				}
				else {
					temp = temp + "[" + i + ",";
					for(int j = 0; j < 5 ; j++) {
						temp = temp + x[i][j].Kolor + "," + x[i][j].Wartosc + ";" ;
					}
					temp += "]";
				}
			}
			out.println(temp);
		}
	}
	
	public void run() {
		String komenda ;
		try
		{
			while(true) {
				komenda = in.readLine();
				System.out.println(komenda);
				if( komenda.equals("G_LEAVE") ) {
					if(this == mRoom.Ad) {
						mSerwer.Pokoje[mRoom.ID] = null;
					}
					if(mRoom.Stol != null) {
						kolejna = -1;
					}
					wymieniono = true;
					mRoom.PlayerOut(this);
					mRoom = null;
				}
				else {
					if( komenda.equals("S_DISCONNECT") ) {
						if(this == mRoom.Ad) {
							mSerwer.Pokoje[mRoom.ID] = null;
						}
						if(mRoom.Stol != null) {
							kolejna = -1;
						}
						wymieniono = true;
						mRoom.PlayerOut(this);
						mRoom = null;
						in.close();
						out.close();
						break;
					}
					else {
						if( komenda.equals("S_REFRESH") ) {
							mSerwer.Refresh(out);
						}
						else {
							if( komenda.contains("S_JOIN_")) {
								if(mRoom == null)
								{
									try
									{
										int x =Integer.parseInt( komenda.substring(7) );
										if(mSerwer.Pokoje[x] != null ) {
											mRoom = mSerwer.Join(x, this , out);
										}
										else {
											out.println("S_ERROR");
										}
									}
									catch(NumberFormatException ex) {
										out.println("S_ERROR");
									}
								}
								else {
									out.println("S_ERROR");
								}
							}
							else {
								if( komenda.contains("S_NEWGAME_")) {
									if(mRoom == null) {
										char kom[] = komenda.toCharArray();
										int ileG = Integer.parseInt( "" + kom[10]);
										int ileB = Integer.parseInt( "" + kom[12]);
										boolean czyA = false;
										if( Integer.parseInt( "" + kom[14]) == 1) {
											czyA= true;
										}
										try {
											mRoom = mSerwer.newRoom(this, czyA , ileG, ileB);
											out.println("S_CREATE");
										} catch (NRoomException e) {
											out.println("S_ERROR");
										}
									}
									else {
										out.println("S_ERROR");
									}
								}
								else {
									if( komenda.equals("G_START")) {
										if(mRoom.iluGraczy == mRoom.iluGraczyMax && this == mRoom.Ad) {
											mSerwer.Pokoje[mRoom.ID] = null;
											mRoom.start();
										}
									}
									else {
										if( komenda.equals("G_NEW") ) {
											//System.out.println("KOLEJNE ROZDANIE!");
											if(mRoom.Stol != null) {
												kolejna = 1;
											}
										}
										else {
											if( komenda.contains("G_WYMIANA_") ) {
												if(komenda.equals("G_WYMIANA")){
													wymieniono = true; 
												}
												else {
													//System.out.println("G_wym");
													char kom[] = komenda.toCharArray() ;
													String temp = "";
													temp += kom[10]; // komenda == G_WYMIANA_X_[...]
													int ile = Integer.parseInt(temp);
													System.out.println(ile);
													if( ile != 0) {
														for(int i = 0; i < ile ; i++) {
															temp = "";
															temp += kom[13 + (i*2)];
															//System.out.println(temp);
															mKarty[Integer.parseInt(temp)] = null;
														}
														
													}
													wymieniono = true;
												}
											}
										}
									}
								}
								
							}
						}
					}
				}
			}
		}
		catch(IOException e) {
			
		}
		catch(NullPointerException e2) {
			try{
				System.out.println("Gracz uciek!");
				mRoom.PlayerOut(this);
				mRoom = null;
			}
			catch(NullPointerException ex1) {
				mRoom = null;
			}
		}
	}
}
